home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / txfilter.cpp < prev    next >
C/C++ Source or Header  |  1999-03-30  |  10KB  |  309 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- // 
  5. // C++ Source Code File Name: txfilter.cpp
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer 
  8. // File Creation Date: 09/17/1997
  9. // Date Last Modified: 03/31/1999
  10. // ----------------------------------------------------------- // 
  11. // ------------- Program Description and Details ------------- // 
  12. // ----------------------------------------------------------- // 
  13. /*
  14. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  15. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  16. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  17. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  18. CORRECTION.
  19.  
  20. Text file filter program used to filter out unwanted characters
  21. from ASCII text files. This program can also be used to convert
  22. DOS text files to a UNIX format of UNIX text files to a DOS
  23. format.
  24. */
  25. // ----------------------------------------------------------- // 
  26. #include <iostream.h>
  27. #include <fstream.h>
  28. #include <string.h>
  29. #include <stdlib.h> 
  30. #include <stdio.h>
  31. #include <ctype.h>
  32.  
  33. // Define this macro for DOS file opens (the default is UNIX)
  34. // #ifndef __MSDOS__
  35. // #define __MSDOS__
  36. // #endif
  37.  
  38. // Txfilter version number and program name
  39. const double TXFilterVersionNumber = 1031.101;
  40. const char *ProgramName = "txfilter";
  41.  
  42. // Program functions
  43. void ProcessArgs(int argc, char *argv[]);
  44. void HelpMessage(const char *program_name, const double version_number);
  45. int ConvertTextFile(ifstream &infile, ostream &stream);
  46.  
  47. // Program globals
  48. const int MAX_LINE = 1024; // Maximum characters per line
  49. int VerboseMode;           // Echo results
  50. char in_file[MAX_LINE];    // Input file to clean
  51. char out_file[MAX_LINE];   // Optional output file name
  52. int UNIX_TEXT_FORMAT = 1;  // Output text file in UNIX format
  53. int DOS_TEXT_FORMAT = 0;   // Output text file in DOS CR/LF format
  54. int filter_text = 1;       // Filter the text
  55. int printable_chars = 1;   // Output all printable characters only
  56. int alpha_chars = 0;       // Output alphabetic characters only
  57. int alpha_num_chars = 0;   // Output alphanumeric characters only
  58. int numeric_chars = 0;     // Output numeric characters only
  59. const int txLF = 0x0A;     // Line feed
  60. const int txCR = 0x0D;     // Carriage return
  61.  
  62. void HelpMessage(const char *program_name, const double version_number)
  63. {
  64.   char vbuffer[255];
  65.   sprintf(vbuffer, "%.3f", version_number);
  66.   cout << endl;
  67.   cout << "ASCII Text file filter program version "
  68.        << vbuffer  << endl;
  69.   cout << "Usage: " << program_name << " [switches] infile.txt "
  70.        << "outfile.txt (optional)" << endl;
  71.   cout << "Switches:  -?      = Display this help message." << endl;
  72.   cout << "           -d      = Output text file in DOS CR/LF format."
  73.        << endl;
  74.   cout << "           -D      = Do not insert line feeds." << endl;
  75.   cout << "           -n      = No text filtering (defaults to filtered)."
  76.        << endl;
  77.   cout << "           -u      = Output text file in UNIX format (default)."
  78.        << endl;
  79.   cout << "           -P      = Output printable characters only (default)."
  80.        << endl;
  81.   cout << "           -A      = Output alphabetic characters only."
  82.        << endl;
  83.   cout << "           -M      = Output alphanumeric characters only."
  84.        << endl;
  85.   cout << "           -N      = Output numeric characters only."
  86.        << endl;
  87.   cout << endl;
  88.   exit(0);
  89. }
  90.  
  91. void ProcessArgs(int argc, char *argv[])
  92. // Process the program's argument list
  93. {
  94.   int i;
  95.   for(i = 1; i < argc; i++ ) {
  96.     if(*argv[i] == '-') {
  97.       char sw = *(argv[i] +1);
  98.       switch(sw) {
  99.     case '?' :
  100.       HelpMessage(ProgramName, TXFilterVersionNumber);
  101.       break;
  102.  
  103.     case 'd' :
  104.       DOS_TEXT_FORMAT = 1;   
  105.       UNIX_TEXT_FORMAT = 0;  
  106.       break;
  107.  
  108.     case 'D':
  109.       DOS_TEXT_FORMAT = 0;   
  110.       UNIX_TEXT_FORMAT = 0;  
  111.       break;
  112.       
  113.     case 'n' :
  114.       filter_text = 0;
  115.       break;
  116.  
  117.     case 'u' :
  118.       DOS_TEXT_FORMAT = 0;   
  119.       UNIX_TEXT_FORMAT = 1;  
  120.       break;
  121.  
  122.     case 'v' :
  123.       VerboseMode = 1;
  124.       break;
  125.  
  126.     case 'P' :
  127.       printable_chars = 1;   // Output all printable characters only
  128.       alpha_chars = 0;       // Output alphabetic characters only
  129.       alpha_num_chars = 0;   // Output alphanumeric characters only
  130.       numeric_chars = 0;     // Output numeric characters only
  131.       break;
  132.  
  133.     case 'A' :
  134.       printable_chars = 0;   // Output all printable characters only
  135.       alpha_chars = 1;       // Output alphabetic characters only
  136.       alpha_num_chars = 0;   // Output alphanumeric characters only
  137.       numeric_chars = 0;     // Output numeric characters only
  138.       break;
  139.  
  140.     case 'M' :
  141.       printable_chars = 0;   // Output all printable characters only
  142.       alpha_chars = 0;       // Output alphabetic characters only
  143.       alpha_num_chars = 1;   // Output alphanumeric characters only
  144.       numeric_chars = 0;     // Output numeric characters only
  145.       break;
  146.  
  147.     case 'N' :
  148.       printable_chars = 0;   // Output all printable characters only
  149.       alpha_chars = 0;       // Output alphabetic characters only
  150.       alpha_num_chars = 0;   // Output alphanumeric characters only
  151.       numeric_chars = 1;     // Output numeric characters only
  152.       break;
  153.  
  154.     default:
  155.       cerr << endl;
  156.       cerr << "Unknown switch " << argv[i] << endl;
  157.       cerr << "Exiting..." << endl;
  158.       cerr << endl;
  159.       exit(0);
  160.       break;
  161.       }
  162.     }
  163.     else { 
  164.       strcpy(in_file, argv[i]);      // Input file name
  165.       if(argv[i+1]) {
  166.     strcpy(out_file, argv[i+1]); // Output file name
  167.     break;
  168.       }
  169.       else
  170.     break;
  171.     }
  172.   }
  173. }
  174.  
  175. int ConvertTextFile(ifstream &infile, ostream &stream)
  176. // Filter all unwanted control characters from the text file
  177. {
  178.   char LineBuffer[MAX_LINE];
  179.   char OutputBuffer[MAX_LINE];
  180.   int i, j;
  181.  
  182.   // Clear the buffers
  183.   for(i = 0; i < MAX_LINE; i++) LineBuffer[i] = '\0';
  184.   for(i = 0; i < MAX_LINE; i++) OutputBuffer[i] = '\0';
  185.   
  186.   while(!infile.eof()) {
  187.     // Read in the file line by line
  188.     for(i = 0, j = 0; i < MAX_LINE && !infile.eof(); i++) {
  189.       infile.get(LineBuffer[i]);
  190.       if(!filter_text) stream << LineBuffer[i];
  191.       
  192.       // Look for the end of line sequence CR/LF
  193.       if((LineBuffer[i] == '\r') || (LineBuffer[i] == '\n')) {
  194.     // Account for extra '\n' in DOS files
  195.     if(LineBuffer[i] == '\r') continue; 
  196.     break;
  197.       }
  198.       
  199.       if(filter_text) { // Output specified character sets only
  200.     if(printable_chars)
  201.       if(isgraph(LineBuffer[i]) || LineBuffer[i] == ' ')
  202.         OutputBuffer[j++] = LineBuffer[i];
  203.     
  204.     if(alpha_chars)
  205.       if(isalpha(LineBuffer[i]) || LineBuffer[i] == ' ')
  206.         OutputBuffer[j++] = LineBuffer[i];
  207.     
  208.     if(alpha_num_chars)
  209.       if(isalnum(LineBuffer[i]) || LineBuffer[i] == ' ')
  210.         OutputBuffer[j++] = LineBuffer[i];
  211.     
  212.     if(numeric_chars)
  213.       if(isdigit(LineBuffer[i]) || LineBuffer[i] == ' ')
  214.         OutputBuffer[j++] = LineBuffer[i];
  215.       }
  216.     }
  217.  
  218.     if(filter_text == 1) {
  219.       if(OutputBuffer[0] != 0)
  220.     stream.write(OutputBuffer, strlen(OutputBuffer));
  221.       if(!infile.eof()) { // Get rid of EOF marker
  222.     char line_feed = (char)txLF;
  223.     char carriage_return = (char)txCR;
  224.     if(DOS_TEXT_FORMAT) {
  225.       stream.write(&carriage_return, 1);
  226.       stream.write(&line_feed, 1);
  227.     }
  228.     if(UNIX_TEXT_FORMAT)
  229.       stream.write(&line_feed, 1);
  230.       }
  231.     }
  232.  
  233.     // Clear the buffer
  234.     for(i = 0; i < MAX_LINE; i++) LineBuffer[i] = '\0';
  235.     for(i = 0; i < MAX_LINE; i++) OutputBuffer[i] = '\0';
  236.   }
  237.   return 1;
  238. }
  239.  
  240. int main(int argc, char *argv[])
  241. // Program's main thread of execution
  242. {
  243.   // If no argument is given print usage message to the screen 1
  244.   if(argc < 2) {
  245.     HelpMessage(ProgramName, TXFilterVersionNumber);
  246.     return(0);
  247.   }
  248.  
  249.   // Process the programs command line arguments
  250.   ProcessArgs(argc, argv);
  251.   
  252.   if(in_file[0] == 0 ) {
  253.     cout << endl;
  254.     cout << "You must specify a valid input file name." << endl;
  255.     cout << endl;
  256.     exit(0);
  257.   }
  258.     
  259. #if defined (__DOS__)
  260.   // In UN